﻿Imports DTO
Imports BUS
Public Class ucTyGia

    Dim tygia As New TyGiaDTO

    Private Sub barbtnThemNH_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles barbtnThemNH.ItemClick
        Dim frm As New frmThemTyGia
        AddHandler frm.LamMoiTGKhiThem, AddressOf LayDSTyGia
        frm.ShowDialog()
    End Sub

    Sub LayDSTyGia()
        GridControl1.DataSource = TyGiaBUS.LayDSTyGia()
    End Sub

    Private Sub ucTyGia_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        barbtnSuaChuaNH.Enabled = False
        barbtnXoaNH.Enabled = False
        barbtnXuatNH.Enabled = False
        LayDSTyGia()
    End Sub

    Private Sub barbtnNapLaiNH_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles barbtnNapLaiNH.ItemClick
        LayDSTyGia()
    End Sub

    Private Sub GridView1_RowClick(sender As Object, e As DevExpress.XtraGrid.Views.Grid.RowClickEventArgs) Handles GridView1.RowClick
        If e.RowHandle >= 0 Then
            barbtnSuaChuaNH.Enabled = True
            barbtnXoaNH.Enabled = True
            tygia.MaTyGia = GridView1.GetRowCellValue(e.RowHandle, GCMaDV).ToString()
            tygia.TenTyGia = GridView1.GetRowCellValue(e.RowHandle, GCTenDV).ToString()
            tygia.TyGiaQuyDoi = Convert.ToDouble(GridView1.GetRowCellValue(e.RowHandle, GCTGQD))
            tygia.MaTT = Convert.ToInt32(GridView1.GetRowCellValue(e.RowHandle, GCQuanLyDV))
        End If
    End Sub

    Private Sub barbtnSuaChuaNH_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles barbtnSuaChuaNH.ItemClick
        Dim frm As New frmThemTyGia
        frm.maTG = tygia.MaTyGia
        frm.tenTG = tygia.TenTyGia
        frm.TGQD = tygia.TyGiaQuyDoi
        frm.maTT = tygia.MaTT
        frm.kt = 1
        frm.Text = "Sửa tỷ giá"
        frm.ShowDialog()
    End Sub

    Private Sub barbtnXoaNH_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles barbtnXoaNH.ItemClick
        Dim pq As PhanQuyenNhonNDDTO = ChucNangBUS.layQuyenCuaND(frmMain.maND, 26)
        If pq.Xoa = True Then
            Try
                If TyGiaBUS.XoaTyGia(tygia) Then
                    DevExpress.XtraEditors.XtraMessageBox.Show("Xóa thành công tỷ giá", "Thông tin", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    LayDSTyGia()
                    HeThongBUS.themNhatKy(frmMain.maND, "Tiền Tệ", "Xóa", tygia.MaTyGia)
                End If
            Catch ex As Exception
                DevExpress.XtraEditors.XtraMessageBox.Show("Tỷ giá này đang sử dụng không thể xóa", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
                TyGiaBUS.DongKetNoi.Close()
            End Try
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Xin lỗi bạn không có quyền này", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Private Sub GridView1_CustomDrawRowIndicator(sender As Object, e As DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs) Handles GridView1.CustomDrawRowIndicator
        If e.RowHandle >= 0 Then
            If e.Info.IsRowIndicator Then
                e.Info.DisplayText = (e.RowHandle + 1).ToString()
            End If
        End If
    End Sub

    Private Sub GridView1_RowStyle(sender As Object, e As DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs) Handles GridView1.RowStyle
        If (e.RowHandle >= 0) Then
            Dim category As Integer = Convert.ToInt32(GridView1.GetRowCellValue(e.RowHandle, GCQuanLyDV))
            If category = 0 Then
                e.Appearance.ForeColor = Color.Red
                e.Appearance.FontStyleDelta = FontStyle.Italic
            End If
        End If
    End Sub
End Class
